<template>
  <main-layout>
    <div class="cart-page">
      <div class="container">
        <h1>购物车</h1>

        <div v-if="$store.state.cart.length > 0" class="cart-content">
          <div class="cart-items">
            <div
              v-for="item in $store.state.cart"
              :key="item.id"
              class="cart-item"
            >
              <div class="item-image">
                <img :src="item.image" :alt="item.name" />
              </div>
              <div class="item-info">
                <h3>{{ item.name }}</h3>
                <p class="item-desc">{{ item.description }}</p>
                <div class="item-tags">
                  <span v-for="tag in item.tags" :key="tag" class="tag">
                    {{ tag }}
                  </span>
                </div>
              </div>
              <div class="item-price">¥{{ item.price }}</div>
              <div class="item-quantity">
                <button
                  @click="decreaseQuantity(item)"
                  :disabled="item.quantity <= 1"
                >
                  -
                </button>
                <span>{{ item.quantity }}</span>
                <button
                  @click="increaseQuantity(item)"
                  :disabled="item.quantity >= item.stock"
                >
                  +
                </button>
              </div>
              <div class="item-total">¥{{ item.price * item.quantity }}</div>
              <button class="remove-btn" @click="removeItem(item)">
                <i class="fas fa-trash"></i>
              </button>
            </div>
          </div>

          <div class="cart-summary">
            <div class="summary-item">
              <span>商品总价</span>
              <span>¥{{ $store.getters.cartTotal }}</span>
            </div>
            <div class="summary-item">
              <span>运费</span>
              <span>免运费</span>
            </div>
            <div class="summary-item discount" v-if="discount > 0">
              <span>优惠</span>
              <span>-¥{{ discount }}</span>
            </div>
            <div class="summary-total">
              <span>应付总额</span>
              <span>¥{{ $store.getters.cartTotal - discount }}</span>
            </div>
            <button class="checkout-btn" @click="checkout">
              立即结算 ({{ $store.getters.cartCount }}件)
            </button>
          </div>
        </div>

        <div v-else class="empty-cart">
          <img
            src="https://giftflowers.com.hk/media/wysiwyg/giftflowers/empty-cart.png"
            alt="空购物车"
          />
          <h2>购物车还是空的</h2>
          <p>快去挑选心仪的商品吧~</p>
          <router-link to="/products" class="continue-shopping">
            继续购物
          </router-link>
        </div>
      </div>
    </div>
  </main-layout>
</template>

<script>
import MainLayout from "@/components/layouts/MainLayout.vue";

export default {
  name: "CartView",
  components: {
    MainLayout,
  },
  data() {
    return {
      discount: 0,
    };
  },
  methods: {
    decreaseQuantity(item) {
      if (item.quantity > 1) {
        item.quantity--;
      }
    },
    increaseQuantity(item) {
      if (item.quantity < item.stock) {
        item.quantity++;
      }
    },
    removeItem(item) {
      if (confirm("确定要删除这个商品吗？")) {
        this.$store.commit("removeFromCart", item);
      }
    },
    checkout() {
      if (!this.$store.state.user) {
        this.$router.push("/login?redirect=/checkout");
      } else {
        this.$router.push("/checkout");
      }
    },
  },
};
</script>

<style scoped>
.cart-page {
  padding: 40px 0;
  background: #f8f9fa;
  min-height: calc(100vh - 200px);
}

.cart-page h1 {
  margin-bottom: 30px;
  color: #333;
}

.cart-content {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 30px;
}

.cart-items {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.cart-item {
  display: grid;
  grid-template-columns: 120px 2fr 1fr 1fr 1fr auto;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
  gap: 20px;
}

.item-image {
  width: 100px;
  height: 100px;
  overflow: hidden;
  border-radius: 4px;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-info h3 {
  font-size: 16px;
  color: #333;
  margin-bottom: 8px;
}

.item-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.item-tags {
  display: flex;
  gap: 8px;
}

.tag {
  padding: 2px 8px;
  background: #ff6b6b;
  color: white;
  border-radius: 4px;
  font-size: 12px;
}

.item-price,
.item-total {
  font-size: 18px;
  color: #ff6b6b;
  font-weight: bold;
}

.item-quantity {
  display: flex;
  align-items: center;
  gap: 10px;
}

.item-quantity button {
  width: 30px;
  height: 30px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  color: #666;
}

.item-quantity button:disabled {
  color: #ccc;
  cursor: not-allowed;
}

.remove-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: #999;
  transition: color 0.3s;
}

.remove-btn:hover {
  color: #ff6b6b;
}

.cart-summary {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  height: fit-content;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  color: #666;
}

.summary-total {
  display: flex;
  justify-content: space-between;
  margin: 20px 0;
  padding-top: 20px;
  border-top: 1px solid #eee;
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.checkout-btn {
  width: 100%;
  padding: 15px;
  background: #ff6b6b;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  transition: background 0.3s;
}

.checkout-btn:hover {
  background: #ff5252;
}

.empty-cart {
  text-align: center;
  padding: 60px 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.empty-cart img {
  width: 200px;
  margin-bottom: 30px;
}

.empty-cart h2 {
  font-size: 24px;
  color: #333;
  margin-bottom: 10px;
}

.empty-cart p {
  color: #666;
  margin-bottom: 30px;
}

.continue-shopping {
  display: inline-block;
  padding: 12px 30px;
  background: #ff6b6b;
  color: white;
  border-radius: 4px;
  transition: background 0.3s;
}

.continue-shopping:hover {
  background: #ff5252;
}

.discount {
  color: #ff6b6b;
}

@media (max-width: 992px) {
  .cart-content {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .cart-item {
    grid-template-columns: 100px 1fr;
    grid-template-areas:
      "image info"
      "image price"
      "quantity total"
      "remove remove";
    gap: 10px;
  }

  .item-image {
    grid-area: image;
    width: 100%;
    height: 100px;
  }

  .item-info {
    grid-area: info;
  }

  .item-price {
    grid-area: price;
  }

  .item-quantity {
    grid-area: quantity;
  }

  .item-total {
    grid-area: total;
    text-align: right;
  }

  .remove-btn {
    grid-area: remove;
    width: 100%;
    height: 40px;
    background: #f8f9fa;
    border-radius: 4px;
  }
}
</style> 